import { deepClone } from '@/utils/index';

const pie_basic = ({
  seriesData = [],
  labelColor = '#00ffff',
  color = [
    // '#66a6ff',
    '#5470c6',
    '#91cc75',
    '#fac858',
    '#ee6666',
    '#73c0de',
    '#3ba272',
    '#fc8452',
    '#9a60b4',
    '#ea7ccc',
  ],
  unit = '',
  roseType = false,
  radius = [0, '65%'],
  fontSize = 14,
  labelPosition = 'outside',
  legend = true,
  legendPosition = '',
  showPercent = false,
  cursor = 'auto',
  isNegative = false, // 是否负数
}) => {
  const defaultConfig = {
    // animationDuration: 3000,
    tooltip: {
      confine: true,
      trigger: 'item',
      formatter: (params) => {
        let valueName = params.data.valueName || '';
        let content =
          (valueName && `<span>${valueName}</span><br />`) +
          `${params.marker}${params.name}&nbsp;&nbsp;` +
          `<span style="font-weight:bold;">${isNegative ? '-' : ''}${params.value} ${unit}</span>` +
          `<span>${showPercent ? `&nbsp;${params.percent}%` : ''}</span><br />`;
        return content;
      },
    },
    legend: legend && {
      orient: ['top', 'bottom'].includes(legendPosition) ? 'horizontal' : 'vertical',
      itemWidth: 35,
      itemWHeight: 10,
      top: legendPosition == 'top' ? '0' : legendPosition == 'bottom' ? null : 'center',
      left: legendPosition == 'left' ? '3%' : legendPosition == 'right' ? null : 'center',
      right: legendPosition == 'right' ? '3%' : legendPosition == 'left' ? null : 'center',
      bottom: legendPosition == 'bottom' ? '0' : legendPosition == 'top' ? null : 'center',
      formatter: function (params) {
        return params;
      },
      textStyle: {
        fontSize: fontSize,
        color: labelColor,
        rich: {
          underline: {
            width: 70,
            height: 0,
            borderWidth: 1,
            borderColor: labelColor,
          },
        },
      },
      itemGap: 15,
      selected: {},
      tooltip: {
        show: true,
      },
    },
    color: color,
    grid: {
      // top: legend ? '50' : '30',
      // left: '4%',
      // right: '4%',
      // bottom: '10',
      // containLabel: true,
      top: legend ? '30' : '5',
      left: '5',
      right: '5',
      bottom: '5',
      containLabel: true,
    },
    series: [
      {
        type: 'pie',
        data: seriesData,
        cursor: cursor,
        radius: radius,
        center:
          legendPosition == 'left'
            ? ['63%', '50%']
            : legendPosition == 'right'
            ? ['37%', '50%']
            : legendPosition == 'top'
            ? ['50%', '55%']
            : legendPosition == 'bottom'
            ? ['50%', '45%']
            : ['50%', '50%'],
        roseType: roseType,
        label: {
          position: labelPosition,
          fontSize: fontSize,
          color: labelColor,
          formatter: `{c}${unit}`,
        },
      },
    ],
  };
  if (showPercent)
    defaultConfig.series.push({
      type: 'pie',
      data: seriesData,
      cursor: cursor,
      radius: radius,
      center:
        legendPosition == 'left'
          ? ['63%', '50%']
          : legendPosition == 'right'
          ? ['37%', '50%']
          : legendPosition == 'top'
          ? ['50%', '55%']
          : legendPosition == 'bottom'
          ? ['50%', '45%']
          : ['50%', '50%'],
      roseType: roseType,
      label: {
        position: labelPosition == 'inside' ? 'outside' : 'inside',
        fontSize: fontSize,
        color: labelColor,
        formatter: `{d}%`,
      },
    });

  const opt = deepClone(defaultConfig);
  return opt;
};

export default {
  pie_basic,
};
